import {ElMessage, ElLoading} from 'element-plus'

let href = window.location.href.split('/').splice(0, 3).join('/')
let ajax = {
    token: sessionStorage.getItem('token'),
    post(api, obj, fn, err) {
        const loading = ElLoading.service({
            lock: true,
            text: '加载中...',
            background: 'rgba(0, 0, 0, 0.7)',
        })
        fetch(this.url2 + api, {
            method: "POST",
            headers: {
                'Content-Type': 'application/json',
                'token': sessionStorage.getItem('token')
            },
            body: JSON.stringify(obj.body)
        }).then(data => {
            loading.close();
            data.json().then(res => {
                if (res.code == 401) {
                    ElMessage.error('登录失效，请重新登录！')
                    window.location.href = href
                }
                fn(res)
            })
        }).catch(er => {
            loading.close();
            err(er)
        })
    },
    post2(api, obj, fn, err) {
        const loading = ElLoading.service({
            lock: true,
            text: '加载中...',
            background: 'rgba(0, 0, 0, 0.7)',
        })
        fetch(this.url2 + api, {
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'token': sessionStorage.getItem('token')
            },
            body: obj.body
        }).then(data => {
            loading.close();
            data.json().then(res => {
                if (res.code == 401) {
                    ElMessage.error('登录失效，请重新登录！')
                    window.location.href = href
                }
                fn(res)
            })
        }).catch(er => {
            loading.close();
            err(er)
        })
    },
    login(obj, fn, err) {
        fetch(this.url + '/systemservice/api/auth/buildtoken' + `?password=${obj.body.password}&username=${obj.body.username}`, {
            method: "POST",
            headers: {
                'Content-Type': 'application/json',
                'token': sessionStorage.getItem('token')
            },
        }).then(data => {
            data.json().then(res => {
                fn(res)
            })
        }).catch(er => {
            err(er)
        })
    },
    /* http://10.48.2.100:9008 */
    /* http://192.168.1.130:10011 */
    /* http://192.168.1.130:10011/backend */
    /* http://192.168.137.1:9008 */
    // url:'http://192.168.86.135:9008',
    // url:'http://192.168.0.4:9008',
    // url:'http://127.0.0.1:100/backend',
    // url2:'http://127.0.0.1:100/backend/zn',
    // url:'http://192.168.137.1:9008',
    // url2:'http://192.168.137.1:9008/zn',
    // url:'http://10.48.2.100:9008',
    // url2:'http://10.48.2.100:9008/zn',

    // url: 'http://192.168.1.130:100/backend',
//      url: 'http://192.168.1.254:100/backend',
//      url2: 'http://192.168.1.254:100/backend/zn',
//     url2: 'http://192.168.1.130:100/backend/zn',

    // url: 'http://192.168.1.130:100/backend',
    // url2: 'http://192.168.1.130:100/backend/zn',

    // url:'http://192.168.1.136:9008',
    // url2:'http://192.168.1.136:9008/zn',

    // url:'http://10.0.0.6:9008',
    // url2:'http://10.0.0.6:9008/zn',

    // url: 'http://10.0.0.15:9008',
    // url2: 'http://10.0.0.15:9008/zn',
    //
    // url: 'http://zn.flechazo.net',
    // url2: 'http://zn.flechazo.net/zn',

    url: 'http://127.0.0.1:9008',
    url2: 'http://127.0.0.1:9008/zn',

    // url:'http://140.210.204.180:9008',
    // url2:'http://140.210.204.180:9008',

    // url:'http://10.48.2.87:9008',
    // url2:'http://10.48.2.87:9008/zn',
    // url:'http://192.168.33.124:9008',
    // url2:'http://192.168.33.124:9008/zn',
    post3(api, obj, fn) {
        fetch(this.url2 + api, {
            method: "POST",
            headers: {
                'Content-Type': 'application/json',
                'token': sessionStorage.getItem('token')
            },
            body: obj.body
        }).then(data => {
            data.json().then(res => {
                if (res.code == 401) {
                    ElMessage.error('登录失效，请重新登录！')
                    window.location.href = href
                }
                fn(res)
            })
        })
    },
}
export default ajax;